<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"sekla.cn","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="这是老版的实验，新版实验已经在2019年更新在CMU官网。 Datalab数据实验1.自定义bits.c文件并用dlc文件自动化编译检查，若代码无误则无返回信息，根据提示运行即可。一旦得到一个合法的解决方案，可以开始测试正误。 2.btest用于测试实验正确性通过运行函数通过测试样例。 实验内容实验提供了3个主要指导。  修改bits.c文件并通过编译。  通过btest测试函数功能正确。  sh">
<meta property="og:type" content="article">
<meta property="og:title" content="CSAPP-datalab">
<meta property="og:url" content="http://sekla.cn/2021/04/15/csapp-datalab/index.html">
<meta property="og:site_name" content="Sekla&#39;s Blog">
<meta property="og:description" content="这是老版的实验，新版实验已经在2019年更新在CMU官网。 Datalab数据实验1.自定义bits.c文件并用dlc文件自动化编译检查，若代码无误则无返回信息，根据提示运行即可。一旦得到一个合法的解决方案，可以开始测试正误。 2.btest用于测试实验正确性通过运行函数通过测试样例。 实验内容实验提供了3个主要指导。  修改bits.c文件并通过编译。  通过btest测试函数功能正确。  sh">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-04-15T11:30:00.000Z">
<meta property="article:modified_time" content="2021-10-18T14:39:42.206Z">
<meta property="article:author" content="Sekla">
<meta property="article:tag" content="二进制">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://sekla.cn/2021/04/15/csapp-datalab/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>CSAPP-datalab | Sekla's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <script type="text/javascript" src="/js/my_js/clicklove.js"></script>
<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Sekla's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Keep Learning Keep Doing</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">100</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://sekla.cn/2021/04/15/csapp-datalab/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Sekla">
      <meta itemprop="description" content="Sekla">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Sekla's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          CSAPP-datalab
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-04-15 19:30:00" itemprop="dateCreated datePublished" datetime="2021-04-15T19:30:00+08:00">2021-04-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-10-18 22:39:42" itemprop="dateModified" datetime="2021-10-18T22:39:42+08:00">2021-10-18</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">计算机系统</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F/CSAPP%E5%AE%9E%E9%AA%8C%E7%B3%BB%E5%88%97/" itemprop="url" rel="index"><span itemprop="name">CSAPP实验系列</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>8.6k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>8 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>这是老版的实验，新版实验已经在2019年更新在CMU官网。</p>
<h2 id="Datalab数据实验"><a href="#Datalab数据实验" class="headerlink" title="Datalab数据实验"></a>Datalab数据实验</h2><p>1.自定义bits.c文件并用dlc文件自动化编译检查，若代码无误则无返回信息，根据提示运行即可。一旦得到一个合法的解决方案，可以开始测试正误。</p>
<p>2.btest用于测试实验正确性通过运行函数通过测试样例。</p>
<h2 id="实验内容"><a href="#实验内容" class="headerlink" title="实验内容"></a>实验内容</h2><p>实验提供了3个主要指导。</p>
<ol>
<li><p>修改bits.c文件并通过编译。</p>
</li>
<li><p>通过btest测试函数功能正确。</p>
</li>
<li><p>show文件帮助数字类型转化与浮点数分解。</p>
<a id="more"></a>

</li>
</ol>
<h3 id="Bits-c规则"><a href="#Bits-c规则" class="headerlink" title="Bits.c规则"></a>Bits.c规则</h3><p>根据整形数字编码规则，我们需要替换在每个函数中的return语句，使用C语言补完函数，根据例子中给出的函数，每个expr都必须是包含0-255范围的整数，不能使用大常数，只能使用局部变量，可以使用一元或二元的的整形运算。有些问题进一步限制了允许的运算符的集合。每个“Expr”可以包含多个运算符。不必一行一个操作符。</p>
<p>应该避免使用</p>
<ol>
<li>控制语句</li>
<li>宏定义</li>
<li>额外函数</li>
<li>调用函数</li>
<li>其他逻辑或判断运算符</li>
<li>任何形式的类型转换</li>
<li>除整形外的任何数据结构与形式</li>
</ol>
<p>假设机器环境为32位使用二进制补码，执行算术右移，发生不可预料的行为当移动数字超过一个字长，即32位。</p>
<p>浮点数则可以使用控制和条件语句，但是除以上整形避免规则外，也不能使用如float类型的数字。</p>
<p><strong>更多注意事项参考README与bits.c注释部分。</strong></p>
<h3 id="正式实验部分"><a href="#正式实验部分" class="headerlink" title="正式实验部分"></a>正式实验部分</h3><h4 id="bitAnd"><a href="#bitAnd" class="headerlink" title="bitAnd"></a>bitAnd</h4><p>实现x&amp;y，只能使用~和|运算符，本来是x，y二进制各个位数相合取，例如6&amp;5=4，二进制形式为0110&amp;0101=0100，0100取反为1011,可以想到对x,y分别取反后析取再取反，对于例子来说1001||1010=1011，取反为0100，联想到基本的逻辑运算德摩根律进行变化即可得到答案，成功通过btest测试。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * bitAnd - x&amp;y using only ~ and | </span></span><br><span class="line"><span class="comment"> *   Example: bitAnd(6, 5) = 4</span></span><br><span class="line"><span class="comment"> *   Legal ops: ~ |</span></span><br><span class="line"><span class="comment"> *   Max ops: 8</span></span><br><span class="line"><span class="comment"> *   Rating: 1</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">bitAnd</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> ~((~x)|(~y));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="getByte"><a href="#getByte" class="headerlink" title="getByte"></a>getByte</h4><p>要求我们从word x中提取byte n，字节数n从0-3分别为最低有效到最高有效。一个字节是8位，可以把要提取的字节进行右移后与0xff相与，可以实现保留最后需要的数字，舍弃不需要的数字，所以取第n个字节，需要右移8<em>n位，8\</em>n根据二进制位运算可知为n&lt;&lt;3,可以写出答案。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * getByte - Extract byte n from word x</span></span><br><span class="line"><span class="comment"> *   Bytes numbered from 0 (LSB) to 3 (MSB)</span></span><br><span class="line"><span class="comment"> *   Examples: getByte(0x12345678,1) = 0x56</span></span><br><span class="line"><span class="comment"> *   Legal ops: ! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</span></span><br><span class="line"><span class="comment"> *   Max ops: 6</span></span><br><span class="line"><span class="comment"> *   Rating: 2</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getByte</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (x&gt;&gt;(n&lt;&lt;<span class="number">3</span>))&amp;<span class="number">0xff</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="logicalShift"><a href="#logicalShift" class="headerlink" title="logicalShift"></a>logicalShift</h4><p>需要进行实现逻辑右移，由于c语言默认为算术右移，所以要特别注意符号数，首先想到的是先算术右移，然后将右移导致多出来的1变成0，这时可以联想到第二个函数的实现，比如我右移了n位，可以找一个左边n位为0，后面为1的数来与算术右移后的数字相&amp;，就可以得到目标数，这个数字想来想去无非就是将1左移31位到32位，然后取反后右移n-1位，不能用减法就用加法替换，但是这样会导致最后一位变成了0，所以要让最后一位变回1，接来下就可以完成函数了。（后来才知道这个相与的数叫掩码）</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * logicalShift - shift x to the right by n, using a logical shift</span></span><br><span class="line"><span class="comment"> *   Can assume that 0 &lt;= n &lt;= 31</span></span><br><span class="line"><span class="comment"> *   Examples: logicalShift(0x87654321,4) = 0x08765432</span></span><br><span class="line"><span class="comment"> *   Legal ops: ! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</span></span><br><span class="line"><span class="comment"> *   Max ops: 20</span></span><br><span class="line"><span class="comment"> *   Rating: 3 </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">logicalShift</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (x&gt;&gt;n)&amp;((((~(<span class="number">1</span>&lt;&lt;<span class="number">31</span>))&gt;&gt;n)&lt;&lt;<span class="number">1</span>)|<span class="number">0x01</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="Bitcount"><a href="#Bitcount" class="headerlink" title="Bitcount"></a>Bitcount</h4><p>要求返回数字中1的个数，个人认为这个很难想到，考虑到0000为0,0001为1,0010为2,0011为3，0100为4，那么肯定要经过一系列运算才能得到有多少个1对应的二进制数，首先拿5实验：</p>
<p>5写满32位为</p>
<p>0000 0000 0000 0000 0000 0000 0000 0101</p>
<p>5在最低4位只有2个1，那么肯定要找出一个掩码来变换，不过我首先想到了分治，将32位分为每4位，可以在4位中先求2位中的1，然后再求4位中的1，那么对于每2位，01,10都是一个1,11是2个1，随便找一个8位数实验：</p>
<p>例如1101 0011，即11 01 00 11,11怎么变成代表2呢，所以要寻找一个掩码，这里卡住想了半天没想出来，只能求助于百度，最后得到思路：</p>
<p>先求2位上的1个数之后，再求4位上的，再求8位上的······，至于为什么用分治，可能是因为操作符不够，例如0101011101，每2位即</p>
<p>01 01 11 01，每两位数出个数之后应该为01 01 10 01，变成4位应该为0010 0011最后8位应改为00000101，</p>
<p>那么2位变4位的掩码01010101····01，首先计算每部分后半段1的个数。</p>
<blockquote>
<p>  01 01 10 01</p>
<p>  &amp;01 01 01 01</p>
<p>  =01 01 00 01 计算每2位中的后一位1的个数</p>
<p>  右移1位00 10 11 00</p>
<p>  &amp;00 00 11 11计算前一位1的个数 </p>
<p>  =00 00 11 00 然后两段相加</p>
<p>  得01 01 01 01即每2位有1个1</p>
</blockquote>
<p>以此类推，4位，16位直到32位可以得到最终1个二进制数字，就是多少个1.</p>
<p>那么掩码01 01 01 ··· 01··等等通过移位即可得到。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * bitCount - returns count of number of 1&#x27;s in word</span></span><br><span class="line"><span class="comment"> *   Examples: bitCount(5) = 2, bitCount(7) = 3</span></span><br><span class="line"><span class="comment"> *   Legal ops: ! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</span></span><br><span class="line"><span class="comment"> *   Max ops: 40</span></span><br><span class="line"><span class="comment"> *   Rating: 4</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">bitCount</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">int</span> result;</span><br><span class="line">  <span class="keyword">int</span> tmp1=<span class="number">0x55</span>|(<span class="number">0x55</span>&lt;&lt;<span class="number">8</span>);</span><br><span class="line">  <span class="keyword">int</span> _tmp1=tmp1|(tmp1&lt;&lt;<span class="number">16</span>);</span><br><span class="line">  <span class="keyword">int</span> tmp2=<span class="number">0x33</span>|(<span class="number">0x33</span>&lt;&lt;<span class="number">8</span>);</span><br><span class="line">  <span class="keyword">int</span> _tmp2=tmp2|(tmp2&lt;&lt;<span class="number">16</span>);</span><br><span class="line">  <span class="keyword">int</span> tmp3=(<span class="number">0x0f</span>)|(<span class="number">0x0f</span>&lt;&lt;<span class="number">8</span>);</span><br><span class="line">  <span class="keyword">int</span> _tmp3=tmp3|tmp3&lt;&lt;<span class="number">16</span>;</span><br><span class="line">  <span class="keyword">int</span> _tmp4=<span class="number">0xff</span>|(<span class="number">0xff</span>&lt;&lt;<span class="number">16</span>);</span><br><span class="line">  <span class="keyword">int</span> _tmp5=<span class="number">0xff</span>|(<span class="number">0xff</span>&lt;&lt;<span class="number">8</span>);</span><br><span class="line">  result=(x&amp;_tmp1)+((x&gt;&gt;<span class="number">1</span>)&amp;_tmp1);</span><br><span class="line">  result=(result&amp;_tmp2)+((result&gt;&gt;<span class="number">2</span>)&amp;_tmp2);</span><br><span class="line">  result=(result+(result&gt;&gt;<span class="number">4</span>))&amp;_tmp3;</span><br><span class="line">  result=(result+(result&gt;&gt;<span class="number">8</span>))&amp;_tmp4;</span><br><span class="line">  result=(result+(result&gt;&gt;<span class="number">16</span>))&amp;_tmp5;</span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="Bang"><a href="#Bang" class="headerlink" title="Bang"></a>Bang</h4><p>返回!x不用!运算符，同样要判断最高位符号，要返回!x的逻辑0,1即判断一个数的位数是否全为零，容易想到一个求相反数的方法，除符号位外按位取反加一，然而0x00000000补码是自己的本身0x8000000也是自己的本身，所以只有0x00与其相反数进行或运算后最高位才是0，所以运算后对最高位进行移位，如果是0则返回1，其他返回0。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * bang - Compute !x without using !</span></span><br><span class="line"><span class="comment"> *   Examples: bang(3) = 0, bang(0) = 1</span></span><br><span class="line"><span class="comment"> *   Legal ops: ~ &amp; ^ | + &lt;&lt; &gt;&gt;</span></span><br><span class="line"><span class="comment"> *   Max ops: 12</span></span><br><span class="line"><span class="comment"> *   Rating: 4 </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">bang</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">int</span> tmp=~x+<span class="number">1</span>;</span><br><span class="line">  <span class="keyword">return</span> ~((tmp|x)&gt;&gt;<span class="number">31</span>)&amp;<span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="tmin"><a href="#tmin" class="headerlink" title="tmin"></a>tmin</h4><p>返回最小的整形二进制补码。即返回0x80000000.</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * tmin - return minimum two&#x27;s complement integer </span></span><br><span class="line"><span class="comment"> *   Legal ops: ! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</span></span><br><span class="line"><span class="comment"> *   Max ops: 4</span></span><br><span class="line"><span class="comment"> *   Rating: 1</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">tmin</span><span class="params">(<span class="keyword">void</span>)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (<span class="number">0x00</span>+<span class="number">1</span>)&lt;&lt;<span class="number">31</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="fitsBits"><a href="#fitsBits" class="headerlink" title="fitsBits"></a>fitsBits</h4><p>如果x能被表示为n位二进制整形补码，返回1。首先知道x肯定能用32位表示，如果是n位数的话，我想扩展成32位肯定是通过符号扩展，所以判断x是否能被n位表示就是符号扩展的逆运算，然后通过比较原x来判断是否能用n位表示，这里需要注意符号位，是否相等可以通过异或来判断。</p>
<p>比如5即0101，n=3的话，移位操作后变成一个负数，所以肯定不可以。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * fitsBits - return 1 if x can be represented as an </span></span><br><span class="line"><span class="comment"> *  n-bit, two&#x27;s complement integer.</span></span><br><span class="line"><span class="comment"> *   1 &lt;= n &lt;= 32</span></span><br><span class="line"><span class="comment"> *   Examples: fitsBits(5,3) = 0, fitsBits(-4,3) = 1</span></span><br><span class="line"><span class="comment"> *   Legal ops: ! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</span></span><br><span class="line"><span class="comment"> *   Max ops: 15</span></span><br><span class="line"><span class="comment"> *   Rating: 2</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fitsBits</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">int</span> tmp=<span class="number">32</span>+~n+<span class="number">1</span>;<span class="comment">//32-n</span></span><br><span class="line">  <span class="keyword">int</span> _x=x&lt;&lt;tmp&gt;&gt;tmp;</span><br><span class="line">  <span class="keyword">return</span> !(_x^x);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="divpwr2"><a href="#divpwr2" class="headerlink" title="divpwr2"></a>divpwr2</h4><p>计算x/(2^n)，向0取整。需要考虑正负的问题，如果x是正数，直接右移n位，但是负数除法，是向负数取整，所以我们需要进行一定操作来使得负数除法向0取整，参考书73页。</p>
<p>根据书上的证明，即(x+(1&lt;&lt;k)-1)&gt;&gt;k,所以首先需要判断正负，将x符号位移到最后一位后，将偏置量bias与之相与，负数为1所以可以得到偏置量，反之无偏置量。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * divpwr2 - Compute x/(2^n), for 0 &lt;= n &lt;= 30</span></span><br><span class="line"><span class="comment"> *  Round toward zero</span></span><br><span class="line"><span class="comment"> *   Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2</span></span><br><span class="line"><span class="comment"> *   Legal ops: ! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</span></span><br><span class="line"><span class="comment"> *   Max ops: 15</span></span><br><span class="line"><span class="comment"> *   Rating: 2</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">divpwr2</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> tmp=x&gt;&gt;<span class="number">31</span>;</span><br><span class="line">    <span class="keyword">int</span> bias=tmp&amp;((<span class="number">1</span>&lt;&lt;n)+(~<span class="number">0</span>));</span><br><span class="line">    <span class="keyword">return</span> (x+bias)&gt;&gt;n;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="negate"><a href="#negate" class="headerlink" title="negate"></a>negate</h4><p>求相反数，按位取反+1</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * negate - return -x </span></span><br><span class="line"><span class="comment"> *   Example: negate(1) = -1.</span></span><br><span class="line"><span class="comment"> *   Legal ops: ! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</span></span><br><span class="line"><span class="comment"> *   Max ops: 5</span></span><br><span class="line"><span class="comment"> *   Rating: 2</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">negate</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> ~x+<span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="isPositive"><a href="#isPositive" class="headerlink" title="isPositive"></a>isPositive</h4><p>如果x&gt;0返回1，返回0如果x&lt;=0。要注意0的符号位为0，!(!x|(x&gt;&gt;31))</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * isPositive - return 1 if x &gt; 0, return 0 otherwise </span></span><br><span class="line"><span class="comment"> *   Example: isPositive(-1) = 0.</span></span><br><span class="line"><span class="comment"> *   Legal ops: ! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</span></span><br><span class="line"><span class="comment"> *   Max ops: 8</span></span><br><span class="line"><span class="comment"> *   Rating: 3</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">isPositive</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> !(!x|(x&gt;&gt;<span class="number">31</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="isLessOrEqual"><a href="#isLessOrEqual" class="headerlink" title="isLessOrEqual"></a>isLessOrEqual</h4><p>判断x小于等于y，如果是返回1，否则0。按位进行比较，首先判断符号位，可以进行x-y运算与0比较，注意如果异号的话，可能导致溢出。</p>
<p>首先x,y右移31位判断符号，然后按照二进制减法得到的结果再判断一次符号位，最后结果分为同号和异号，同号即signx^signy=0，若signy_x符号位为1，则x小于y，返回1，异号则signx^signy=1,signy_x符号位为1,其他同理可得，根据逻辑表达式化简可得返回值。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * isLessOrEqual - if x &lt;= y  then return 1, else return 0 </span></span><br><span class="line"><span class="comment"> *   Example: isLessOrEqual(4,5) = 1.</span></span><br><span class="line"><span class="comment"> *   Legal ops: ! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</span></span><br><span class="line"><span class="comment"> *   Max ops: 24</span></span><br><span class="line"><span class="comment"> *   Rating: 3</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">isLessOrEqual</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">int</span> signx=(x&gt;&gt;<span class="number">31</span>)&amp;<span class="number">1</span>;</span><br><span class="line">  <span class="keyword">int</span> signy=(y&gt;&gt;<span class="number">31</span>)&amp;<span class="number">1</span>;</span><br><span class="line">  <span class="keyword">int</span> signy_x=((y+~x+<span class="number">1</span>)&gt;&gt;<span class="number">31</span>)&amp;<span class="number">1</span>;</span><br><span class="line">  <span class="keyword">int</span> result=((!signx^signy)&amp;!signy_x)|((signx^signy)&amp;signx);</span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="ilog2"><a href="#ilog2" class="headerlink" title="ilog2"></a>ilog2</h4><p>求以二为底的对数，容易想到二进制数其中如0100为$2^2$，实际上该函数就是求1在二进制数的第几位，所以重点是判断1的位置在哪，要确定1的位置，同样可以运用分治的思想，首先将32位二进制数分为左16位，右16位，然后确定一个标志符，来判断在左在右，一旦判断有1的存在，标志符置1，然后依次来判断8位，4位，2位，最后得到1的引索。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * ilog2 - return floor(log base 2 of x), where x &gt; 0</span></span><br><span class="line"><span class="comment"> *   Example: ilog2(16) = 4</span></span><br><span class="line"><span class="comment"> *   Legal ops: ! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</span></span><br><span class="line"><span class="comment"> *   Max ops: 90</span></span><br><span class="line"><span class="comment"> *   Rating: 4</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">ilog2</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">int</span> sign,index1,index2,index3,index4;</span><br><span class="line">  sign=!!(x&gt;&gt;<span class="number">16</span>);</span><br><span class="line">  index1=sign&lt;&lt;<span class="number">4</span>;</span><br><span class="line">  x=x&gt;&gt;index1;</span><br><span class="line">  sign=!!(x&gt;&gt;<span class="number">8</span>);</span><br><span class="line">  index2=sign&lt;&lt;<span class="number">3</span>;</span><br><span class="line">  x=x&gt;&gt;index2;</span><br><span class="line">  sign=!!(x&gt;&gt;<span class="number">4</span>);</span><br><span class="line">  index3=sign&lt;&lt;<span class="number">2</span>;</span><br><span class="line">  x=x&gt;&gt;index3;</span><br><span class="line">  sign=!!(x&gt;&gt;<span class="number">2</span>);</span><br><span class="line">  index4=sign&lt;&lt;<span class="number">1</span>;</span><br><span class="line">  x=x&gt;&gt;index4;</span><br><span class="line">  <span class="keyword">return</span> index1+index2+index3+index4;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="float-eng"><a href="#float-eng" class="headerlink" title="float_eng"></a>float_eng</h4><p>当数为NaN时，返回参数，参数和结果都用无符号整形来传递，不为NaN时，返回负的参数。但是他们用来表示一个单精度浮点数，NaN判断要判断阶码段是否都为1，小数段是否非0，其他非NaN的数，改变符号位即可。有8位阶码，23位frac</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * float_neg - Return bit-level equivalent of expression -f for</span></span><br><span class="line"><span class="comment"> *   floating point argument f.</span></span><br><span class="line"><span class="comment"> *   Both the argument and result are passed as unsigned int&#x27;s, but</span></span><br><span class="line"><span class="comment"> *   they are to be interpreted as the bit-level representations of</span></span><br><span class="line"><span class="comment"> *   single-precision floating point values.</span></span><br><span class="line"><span class="comment"> *   When argument is NaN, return argument.</span></span><br><span class="line"><span class="comment"> *   Legal ops: Any integer/unsigned operations incl. ||, &amp;&amp;. also if, while</span></span><br><span class="line"><span class="comment"> *   Max ops: 10</span></span><br><span class="line"><span class="comment"> *   Rating: 2</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">unsigned</span> <span class="title">float_neg</span><span class="params">(<span class="keyword">unsigned</span> uf)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">unsigned</span> <span class="built_in">exp</span>=uf&gt;&gt;<span class="number">23</span>&amp;<span class="number">0xff</span>;</span><br><span class="line">  <span class="keyword">unsigned</span> tmp=uf&amp;(<span class="number">0x7fffff</span>);</span><br><span class="line">  <span class="keyword">if</span>(<span class="built_in">exp</span>==<span class="number">0xff</span>&amp;&amp;frac!=<span class="number">0</span>)</span><br><span class="line">    <span class="keyword">return</span> uf;</span><br><span class="line">  <span class="keyword">return</span> uf^(<span class="number">1</span>&lt;&lt;<span class="number">31</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="float-i2f"><a href="#float-i2f" class="headerlink" title="float_i2f"></a>float_i2f</h4><p>类型转换，把int型转换为float型，要考虑的东西很多，首先注意是unsigned，所以左移右移都应该补0，再是转化精度的问题，要向偶数舍入，然后将整形左移。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * float_i2f - Return bit-level equivalent of expression (float) x</span></span><br><span class="line"><span class="comment"> *   Result is returned as unsigned int, but</span></span><br><span class="line"><span class="comment"> *   it is to be interpreted as the bit-level representation of a</span></span><br><span class="line"><span class="comment"> *   single-precision floating point values.</span></span><br><span class="line"><span class="comment"> *   Legal ops: Any integer/unsigned operations incl. ||, &amp;&amp;. also if, while</span></span><br><span class="line"><span class="comment"> *   Max ops: 30</span></span><br><span class="line"><span class="comment"> *   Rating: 4</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">unsigned</span> <span class="title">float_i2f</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(x==<span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> x;</span><br><span class="line">    <span class="keyword">int</span> MSB,<span class="built_in">exp</span>,frac,sign,flag;</span><br><span class="line">    <span class="keyword">unsigned</span> tmp,result;</span><br><span class="line">    sign=(x&gt;&gt;<span class="number">31</span>)&amp;<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span>(sign)<span class="comment">//若x为负数</span></span><br><span class="line">        x=~x+<span class="number">1</span>;<span class="comment">//取x相反数变为正数</span></span><br><span class="line">    	MSB=<span class="number">0</span>;</span><br><span class="line">    	tmp=x;</span><br><span class="line">    <span class="keyword">while</span>(<span class="number">1</span>)&#123;</span><br><span class="line">        tmp&lt;&lt;=<span class="number">1</span>;<span class="comment">//每次移动tmp左1位</span></span><br><span class="line">        MSB++;</span><br><span class="line">        <span class="keyword">if</span>(tmp&amp;<span class="number">0x80000000</span>) <span class="keyword">break</span>;</span><br><span class="line">    &#125;<span class="comment">//获得x最高位，来确定frac</span></span><br><span class="line">    tmp=tmp&lt;&lt;<span class="number">1</span>;</span><br><span class="line">    <span class="built_in">exp</span>=<span class="number">127</span>+<span class="number">31</span>-MSB;<span class="comment">//注意偏置值</span></span><br><span class="line">    frac=<span class="number">31</span>-MSB;</span><br><span class="line">    flag=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span>((tmp&amp;<span class="number">0x1ff</span>)&gt;<span class="number">0x100</span>)</span><br><span class="line">        flag=<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span>((tmp&amp;<span class="number">0x3ff</span>)==<span class="number">0x300</span>)</span><br><span class="line">        flag=<span class="number">1</span>;<span class="comment">//舍入</span></span><br><span class="line">    result=(MSB&lt;&lt;<span class="number">31</span>)+(<span class="built_in">exp</span>&lt;&lt;<span class="number">23</span>)+(tmp&gt;&gt;<span class="number">9</span>)+flag;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="float-twice"><a href="#float-twice" class="headerlink" title="float_twice"></a>float_twice</h4><p>计算2*f的值，注意非规格化与规格化数，阶码为0位规格化数，尾数域左移1位，如果是规格化数，阶码+1即可。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"> * float_twice - Return bit-level equivalent of expression 2*f for</span></span><br><span class="line"><span class="comment"> *   floating point argument f.</span></span><br><span class="line"><span class="comment"> *   Both the argument and result are passed as unsigned int&#x27;s, but</span></span><br><span class="line"><span class="comment"> *   they are to be interpreted as the bit-level representation of</span></span><br><span class="line"><span class="comment"> *   single-precision floating point values.</span></span><br><span class="line"><span class="comment"> *   When argument is NaN, return argument</span></span><br><span class="line"><span class="comment"> *   Legal ops: Any integer/unsigned operations incl. ||, &amp;&amp;. also if, while</span></span><br><span class="line"><span class="comment"> *   Max ops: 30</span></span><br><span class="line"><span class="comment"> *   Rating: 4</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">unsigned</span> <span class="title">float_twice</span><span class="params">(<span class="keyword">unsigned</span> uf)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">if</span>((uf&amp;<span class="number">7f</span>800000)==<span class="number">0</span>)</span><br><span class="line">    uf=((uf&amp;<span class="number">0x007fffff</span>)&lt;&lt;<span class="number">1</span>)|(uf&amp;<span class="number">0x80000000</span>);</span><br><span class="line">  <span class="keyword">else</span> <span class="keyword">if</span>((uf&amp;<span class="number">0x7f800000</span>)!=<span class="number">0x7f800000</span>)</span><br><span class="line">    uf=uf+<span class="number">0x800000</span>;     </span><br><span class="line">  <span class="keyword">return</span> uf;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>
    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/%E4%BA%8C%E8%BF%9B%E5%88%B6/" rel="tag"><i class="fa fa-tag"></i> 二进制</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/04/14/csapp-lab0/" rel="prev" title="gdb基本操作等简单指令">
      <i class="fa fa-chevron-left"></i> gdb基本操作等简单指令
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/04/15/csapp-datalabafter/" rel="next" title="DataLab后考证">
      DataLab后考证 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Datalab%E6%95%B0%E6%8D%AE%E5%AE%9E%E9%AA%8C"><span class="nav-number">1.</span> <span class="nav-text">Datalab数据实验</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E9%AA%8C%E5%86%85%E5%AE%B9"><span class="nav-number">2.</span> <span class="nav-text">实验内容</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Bits-c%E8%A7%84%E5%88%99"><span class="nav-number">2.1.</span> <span class="nav-text">Bits.c规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%AD%A3%E5%BC%8F%E5%AE%9E%E9%AA%8C%E9%83%A8%E5%88%86"><span class="nav-number">2.2.</span> <span class="nav-text">正式实验部分</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#bitAnd"><span class="nav-number">2.2.1.</span> <span class="nav-text">bitAnd</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#getByte"><span class="nav-number">2.2.2.</span> <span class="nav-text">getByte</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#logicalShift"><span class="nav-number">2.2.3.</span> <span class="nav-text">logicalShift</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Bitcount"><span class="nav-number">2.2.4.</span> <span class="nav-text">Bitcount</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Bang"><span class="nav-number">2.2.5.</span> <span class="nav-text">Bang</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#tmin"><span class="nav-number">2.2.6.</span> <span class="nav-text">tmin</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#fitsBits"><span class="nav-number">2.2.7.</span> <span class="nav-text">fitsBits</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#divpwr2"><span class="nav-number">2.2.8.</span> <span class="nav-text">divpwr2</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#negate"><span class="nav-number">2.2.9.</span> <span class="nav-text">negate</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#isPositive"><span class="nav-number">2.2.10.</span> <span class="nav-text">isPositive</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#isLessOrEqual"><span class="nav-number">2.2.11.</span> <span class="nav-text">isLessOrEqual</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#ilog2"><span class="nav-number">2.2.12.</span> <span class="nav-text">ilog2</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#float-eng"><span class="nav-number">2.2.13.</span> <span class="nav-text">float_eng</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#float-i2f"><span class="nav-number">2.2.14.</span> <span class="nav-text">float_i2f</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#float-twice"><span class="nav-number">2.2.15.</span> <span class="nav-text">float_twice</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Sekla"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Sekla</p>
  <div class="site-description" itemprop="description">Sekla</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">100</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/ShenTiao" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ShenTiao" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:584892986@qq.com" title="E-Mail → mailto:584892986@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

      <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1416875904&auto=0&height=66"></iframe>
      
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Sekla</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">288k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">4:22</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":250,"height":500},"mobile":{"show":false},"react":{"opacity":0.9},"log":false});</script></body>
</html>

<script type="text/javascript" src="/js/src/love.js"></script>
